﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using Sustainalytics.ClientDW.Synchronization.SyncHbaseData;
using Sustainalytics.ClientDW.Synchronization.SyncHbaseData.Models;

using Xunit;

namespace GetHbaseDataTests
{
    public class GetDataFromHbaseTests
    {
        protected GetHbaseData GetHbaseData;
        public GetDataFromHbaseTests()
        {
            GetHbaseData = new GetHbaseData(CallbackMessages);
        }

        protected void CallbackMessages(string concurrentBag)
        {
            Console.WriteLine(concurrentBag);
        }

        public class GetDataFromHbase : GetDataFromHbaseTests
        {

            [Fact]
            public void Should_ReturnCompanies()
            {
                var allCompanies = GetHbaseData.GetCompactCorporateDetails();
                
                Assert.NotEmpty(allCompanies);
            }
            [Fact]
            public void CoverageCompaniesResetPU()
            {
                var allCompanies = GetHbaseData.GetCompactCorporateDetails();
                var parentCompany = allCompanies.FirstOrDefault(x => x.Id == Guid.Parse("d3533cbe-cd5c-4198-b7fa-f79502c53d36"));
                var coverageCompany = allCompanies.FirstOrDefault(x => x.ParentResearchId == Guid.Parse("d3533cbe-cd5c-4198-b7fa-f79502c53d36"));

                Assert.NotEmpty(allCompanies);
            }
            [Fact]
            public void GetIdentifiersForCompanyId()
            {
                var allCompanies = GetHbaseData.GetCompactCorporateDetails();

                var chevron = allCompanies.Where(x => x.Id == Guid.Parse("6c6845ff-4d9e-4f4d-b920-962af834ef0a"));
                //var corporateDetails = GetHbaseData.ListCompanies(companyIds.ToList());
                CorporateDetailsTransformation identifiers = GetHbaseData.GetCorporateDetails(new List<string>()
                {
                    "98506",
                });
                //NUUID("6c6845ff-4d9e-4f4d-b920-962af834ef0a")
                int i = 1;
            }
            [Fact]
            public void Should_ReturnRegions()
            {
                var allRegions = GetHbaseData.GetRegions();
                Assert.NotEmpty(allRegions);
            }
            [Fact]
            public void Should_ReturnCountries()
            {
                var allCountries = GetHbaseData.GetCountries();
                Assert.NotEmpty(allCountries);
            }
            [Fact]
            public void Should_ReturnCompanyIdentifierTypes()
            {
                var allCorporateIdentifierTypes = GetHbaseData.GetCorporateIdentifierTypes();
                Assert.NotEmpty(allCorporateIdentifierTypes);
            }

            [Fact]
            public void Should_ReturnCorporateGovernanceRegions()
            {
                var allCorporateIdentifierTypes = GetHbaseData.GetCorporateGovernanceRegions();
                Assert.NotEmpty(allCorporateIdentifierTypes);
            }

            //[Fact]
            [Theory()]
            [InlineData(1000,50)]
            [InlineData(3000, 50)]
            [InlineData(5000, 50)]
            [InlineData(10000, 50)]
            [InlineData(15000, 50)]
            public void ShouldReceiveSecurities_OnlyForSelectedCompanies(int skip, int take)
            {
                var compactCorporateDetails = GetHbaseData.GetCompactCorporateDetails();
                Assert.NotEmpty(compactCorporateDetails);
              
               var corporateDetailsTransformation = GetHbaseData.GetCorporateDetails(compactCorporateDetails.Skip(skip).Take(take).ToList().Select(x => x.CapitalIqId));

                Assert.Equal(corporateDetailsTransformation.CorporateDetails.Count(), take);
                //every security has a company id in corporate detail
                foreach (var security in corporateDetailsTransformation.Securities)
                {
                    var companyForSecurity = corporateDetailsTransformation.CorporateDetails.FirstOrDefault(x => x.CompanyId == security.CompanyId);
                    Assert.NotNull(companyForSecurity);
                }
            }
        }
    }
}
